package org.sikyon.chocolatebiscuit.model.entities;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import javax.annotation.concurrent.Immutable;

@Immutable
public final class User extends SelfContainedIdentity<String> {

    private String email;

    User(String email) {
        super();
        email(email);
    }

    @Override
    protected String idProperty() {
        return "email";
    }

    public String email() {
        return email;
    }


    private void email(String email) {
//        Preconditions.checkNotNull(email);
        // TODO the email CAN'T be null - TDD
        // TODO add regex string matching - TDD
        this.email = email;
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof User)) {
            return false;
        }
        if (this == other) {
            return true;
        }
        User otherUser = (User) other;
        return new EqualsBuilder().append(email(), otherUser.email()).isEquals();
    }

    @Override
    public int hashCode() {
        // TODO cache the hashcode
        return new HashCodeBuilder().append(email()).toHashCode();
    }

}
